//
//  NXVolumeObserver.m
//  YOSticker
//
//  Created by 王成 on 14/10/31.
//  Copyright (c) 2014年 YOSticker. All rights reserved.
//

#import "NXVolumeObserver.h"

#define VOLUME_CHANGED_KEY @"AVSystemController_SystemVolumeDidChangeNotification"

@implementation NXVolumeObserver

+ (NXVolumeObserver *)sharedInstance;
{
    static NXVolumeObserver *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[NXVolumeObserver alloc] init];
    });
    
    return instance;
}

-(id)init
{
    if(self = [super init])
    {
        
        MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, -100, 200, 20)];
//        volumeView.center = CGPointMake(150,370);
        [volumeView sizeToFit];
        
        [[UIApplication sharedApplication].keyWindow addSubview:volumeView];
        
        AudioSessionInitialize(NULL, NULL, NULL, NULL);
        SInt32  process = kAudioSessionCategory_AmbientSound;
        AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(process), &process);
        AudioSessionSetActive(YES);
 
    }
    return self;
}

- (void)startObserveVolumeChangeEvents
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(volumeChanged:)
                                                 name:VOLUME_CHANGED_KEY
                                               object:nil];
    
}

- (void)stopObserveVolumeChangeEvents
{
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:VOLUME_CHANGED_KEY];
    
}

- (void)volumeChanged:(NSNotification *)notification
{
    float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
    
    NSLog(@"************now volumn is %f", volume);
    
    //发出通知
    if(self.delegate && [self.delegate respondsToSelector:@selector(volumeButtonDidClick:)])
    {
        [self.delegate volumeButtonDidClick:self];
    }
    
}
@end
